package com.mach.platform.controller;

import com.mach.platform.domain.Response;
import com.mach.platform.domain.test.JobTestTi;
import com.mach.platform.util.SqlHelper;
import com.mach.platform.utils.CacheUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.controller
 * @time 2018/6/12 0012 15:15
 */
@Api(tags = { "测试接口" })
@RestController
@RequestMapping(value = "test")
public class TestCtrl {

    @Autowired
    private DataSource dataSource;


    @RequestMapping("/cleanCache")
    public Response cleanCache(){
        CacheUtils.guavaCacheFactoryEMS.invalidateAll();
        CacheUtils.guavaCacheFactoryEMS.stats();
        return Response.ok();
    }
    @RequestMapping("/getSkillmData")
    public Response getJobTestData(@RequestParam Map<String,Object> param){
        HashMap<Integer ,JobTestTi> rDatas = new HashMap<Integer ,JobTestTi>();
        SqlHelper sqlHelper = SqlHelper.getBySource(this.dataSource);

        try {

            Map<Object, Map<String, Object>> dbData = sqlHelper.query(
                    "select num,gan,anop,antxt,xy" +
                            " from test_skillm ");

            Collection<Map<String, Object>> dbValues = dbData.values();
            for(Map<String, Object> item : dbValues){
                Integer num =(Integer)item.get("num");
                JobTestTi rItem = rDatas.get(num);
                if(rItem == null){
                    rItem = new JobTestTi();
                    rDatas.put(num, rItem);
                }
                rItem.setNum((Integer)item.get("num"));

                rItem.getAnop().put((String)item.get("anop"), (String)item.get("antxt"));
                rItem.getAnxy().put((String)item.get("anop"), (String)item.get("xy"));

                String gan = (String)item.get("gan");
                if(gan != null && !"".equals(gan.trim())){
                    rItem.setGan(gan);
                }


            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        Response resp =  Response.ok(rDatas);
//        resp.put("data", rDatas);
        return resp;
    }



}
